/*
 * FastDataApproxImpl.cpp
 *
 *  Created on: Jan 8, 2013
 *      Author: Frantisek Schneider
 */

#include "FastDataApproxImpl.h"
#include "stdio.h"

FastDataApproxImpl::FastDataApproxImpl(data* dataItems)
{
	this->dataItems = dataItems;
}

FastDataApproxImpl::~FastDataApproxImpl()
{
	delete dataItems;
}

HRESULT WINAPI FastDataApproxImpl::GetBuf(LPAPPROXPOINT points, floattype step,	int len, int *filled)
{

	if (step <= 0 || points == NULL)
	{
		return E_INVALIDARG;
	}

	if (len > (int) (dataItems->length))
	{
		return S_FALSE;
	}

	double time = 0;
	int filledCount = 0;
	for (int i = 0; i < len; i++)
	{
		points[i].x = time;
		int precPoint = -1;
		GetPrecedingPoint(time, &precPoint);
		points[i].y = dataItems->items[precPoint].ig;
		time += step;
		filledCount++;
	}

	*filled = filledCount;
	return S_OK;
}

HRESULT WINAPI FastDataApproxImpl::GetPrecedingPoint(floattype x, int *index)
{
	int start, end, midPt;			// binary search
	start = 0;
	end = dataItems->length;

	while (start <= end)
	{
		midPt = (start + end) / 2;
		if (dataItems->items[midPt].t == x)
		{
			index = (int*) midPt;
			return S_OK;
		}
		else if (dataItems->items[midPt].t < x)
		{
			start = midPt + 1;
		}
		else
		{
			end = midPt - 1;
		}
	}
	return S_FALSE;
}

HRESULT WINAPI FastDataApproxImpl::Count(int *cnt)
{
	return E_NOTIMPL;
}

HRESULT WINAPI FastDataApproxImpl::Clear(int start, int stop)
{
	return E_NOTIMPL;
}
HRESULT WINAPI FastDataApproxImpl::AddBuf(LPAPPROXPOINT points, int len, int *added)
{
    return (E_NOTIMPL);
}

HRESULT WINAPI FastDataApproxImpl::EnumBuf(int *index, LPAPPROXPOINT points, int len,  int *filled)
{
	return E_NOTIMPL;
}
